package com.longteng.lesson.controller;

import com.longteng.lesson.dao.UserMapper;
import com.longteng.lesson.entity.MemberObj;
import com.longteng.lesson.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator
 * DATE : 2018/6/24
 * TIME : 10:17
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@RestController
@RequestMapping("/dbController")
@Slf4j
public class DbController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/getAllUser")
    public List<User> getAllUser(User user) {
        log.info("info获取所有的用户");
        log.error("error错误信息");
        log.debug("debug日志");
        return userMapper.getAllUser(user);
    }
//
//    @RequestMapping("/getUserById")
//    public User getUserById(int id) {
//        return userMapper.findUserById(id);
//    }
//
//    @RequestMapping("/getUserByEmail")
//    public User findUserByEmail(String email) {
//        User user = new User();
//        user.setEmail(email);
//        return userMapper.findUserByEmail(user);
//    }
//
//    @RequestMapping("/getUserByIdAndName")
//    public User getUserByIdAndName(int id, String name) {
//        return userMapper.findUserByIdAndName(id, name);
//    }
    @RequestMapping("/getUserByColumn")
    public List<User> findUserByColumn(User user) {
        List<User> user1 =userMapper.findUserByColumn(user);
        return user1;
    }
    @RequestMapping("/findUserByUserName")
    public List<User> findUserByUserName(String name) {
        return userMapper.findUserByUserName(name);
    }
    @RequestMapping("/findUserAndMember")
    public User findUserAndMember(User user) {
        return userMapper.findUserAndMember(user);
    }

    @RequestMapping("/updateUser")
    public String updateUser(User user) {
        userMapper.updateUser(user);
        return  "success";
    }


    @RequestMapping("/addUser")
    @Transactional
    public Integer addUser(User user, MemberObj member) {
        userMapper.addUser(user);
        member.setUserId(user.getId());

        userMapper.addMember(member);
        return user.getId();
    }
    @RequestMapping("/addAllUser")
    public String addUser() {
        List<User> list=new ArrayList();
        User user=new User();
        user.setUserName("a");
        user.setAge(20);
        list.add(user);
        User user1 =new User();
        user1.setUserName("b");
        user1.setAge(21);
        list.add(user1);
        userMapper.insertAllUser(list);
        return "success";
    }


    @RequestMapping("/updateAllUserById")
    public String updateAllUserById() {
        List<User> list=new ArrayList();
        User user=new User();
        user.setUserName("aaaaaa");
        user.setAge(220);
        user.setId(8);
        list.add(user);
        User user1 =new User();
        user1.setUserName("bbbbb");
        user1.setAge(221);
        user1.setId(9);
        list.add(user1);
        userMapper.updateAllUserById(list);



        return "success";
    }
}
